// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: 'applet-work-2grfv6s9025afe0b'
})

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const db = cloud.database();
  const studentcollection = db.collection('student')
  let openid = wxContext.OPENID;
  console.log(event)
  let { sn, name, nickName, avatarUrl } = event;
  let res = await studentcollection.where({
    sn: sn,
    name: name
  }).get()
  console.log(res)
  let user = {};
  let reg = "ok";
  let errMsg = "";
  if (res.data.length == 0) {
    reg = 'err'
    errMsg = "没找到学号或姓名";
  } else {
    let { _id } = res.data[0]
    let student = res.data[0]
    if (!res.data[0].nickName) {
      res = studentcollection.doc(_id).update({
        data: {
          nickName, avatarUrl, openid
        }
      })
      user ={...student,nickName,avatarUrl,openid};
    } else {
      reg = 'err';
      errMsg = '学号:' + sn + '已被' + res.data[0].nickName + "绑定"
    }
  }

  return {
   reg,errMsg,user
  }
}